<template>
	<!--总容器-->
	<div class="wrapper">

		<!--header-->
		<header>
			<div class="back">
				<p @click="goBack">&lt;</p>
			</div>
			<p>商家列表</p>
		</header>

		<!--body-->
		<ul class="business">
			<li v-for="item in businessArr" @click="toBusinessInfo(item.businessId)">
				<div class="bsns-img">
					<img :src="item.businessImg">
					<div class="bsns-img-quantify" v-show="item.quantity>0">{{item.quantity}}</div>
				</div>
				<div class="bsns-info">
					<h3>{{item.businessName}}</h3>
					<p>&#165;{{item.starPrice}}起送 | &#165;{{item.deliveryPrice}}配送</p>
					<p>{{item.businessExplain}}</p>
				</div>
			</li>

		</ul>

		<Footer></Footer>

	</div>


</template>


<script>
	import Footer from '../components/Footer.vue';

	export default {
		name: 'BusinessList',
		data() {
			return {
				orderTypeId: this.$route.query.orderTypeId,
				businessArr: [],
				user: {}
			}
		},
		created() {
			this.user = this.$getSessionStorage('user');
			this.$axios.post('BusinessController/listBusinessByOrderTypeId', this.$qs.stringify({
				orderTypeId: this.orderTypeId
			})).then(response => {
				this.businessArr = response.data;
				//判断是否登录
				if (this.user != null) {
					this.listCart();
				}
			}).catch(error => {
				console.error(error);
			});
		},
		components: {
			Footer
		},
		methods: {
			goBack() {
				this.$router.go(-1);
			},
			listCart() {
				this.$axios.post('CartController/listCart', this.$qs.stringify({
					userId: this.user.userId
				})).then(response => {
					let cartArr = response.data;
					//遍历所有食品列表
					for (let businessItem of this.businessArr) {
						businessItem.quantity = 0;
						for (let cartItem of cartArr) {
							if (cartItem.businessId == businessItem.businessId) {
								businessItem.quantity += cartItem.quantity;
							}
						}
					}
					this.businessArr.sort();
				}).catch(error => {
					console.error(error);
				});
			},
			toBusinessInfo(businessId) {
				this.$router.push({
					path: '/businessInfo',
					query: {
						businessId: businessId
					}
				});
			}
		}
	}
</script>

<!--
<script>
import { ref, onMounted, getCurrentInstance } from 'vue';
import Footer from "@/components/Footer.vue";
import { useRoute, useRouter } from 'vue-router';

export default {
  name: 'BusinessList',
  components: {
    Footer
  },
  setup() {
    const { appContext } = getCurrentInstance(); // 获取当前组件实例上下文
    const route = useRoute();
    const orderTypeId = ref(null);
    const businessArr = ref([]);
    const router = useRouter();

    const gotoPreviousPage = () => {
      router.back();
    };

    const toBusinessInfo = (businessId) => {
      router.push({ path: '/businessInfo', query: { businessId: businessId } });
    };

    const listCart = (userId) => {
      appContext.config.globalProperties.$axios
        .post('CartController/listCart', appContext.config.globalProperties.$qs.stringify({ userId: userId }))
        .then(response => {
          const cartArr = response.data;
          // 遍历商家列表
          for (const businessItem of businessArr.value) {
            businessItem.quantity = 0;
            for (const cartItem of cartArr) {
              if (cartItem.businessId === businessItem.businessId) {
                businessItem.quantity += cartItem.quantity;
              }
            }
          }
          businessArr.value.sort();
        })
        .catch(error => {
          console.error(error);
        });
    };

    onMounted(() => {
      orderTypeId.value = route.query.orderTypeId;
      const requestData = {
        orderTypeId: orderTypeId.value
      };
      const user = window.sessionStorage.getItem('user'); // 使用 window.sessionStorage 获取数据
      if (user !== null) {
        listCart(JSON.parse(user).userId);
      }

      appContext.config.globalProperties.$axios
        .post('BusinessController/listBusinessByOrderTypeId', appContext.config.globalProperties.$qs.stringify(requestData))
        .then(response => {
          businessArr.value = response.data;
        })
        .catch(error => {
          console.error(error);
        });
    });

    return {
      orderTypeId,
      businessArr,
      gotoPreviousPage,
      toBusinessInfo
    };
  }
};
</script>
-->
<style scoped>
	/****************总容器*********************/
	.wrapper {
		width: 100%;
		height: 100%;
	}
	
	/****************header*********************/
	.wrapper header {
		width: 100%;
		height: 12vw;
		background-color: #0097FF;
		color: #FFFFFF;
		font-size: 4.8vw;

		position: fixed;
		left: 0;
		top: 0;
		z-index: 1000;

		display: flex;
		justify-content: center;
		align-items: center;
	}

	.wrapper header .back {
		position: absolute;
		width: 2vw;
		height: 2vw;
		left: 1.5vw;
		top: 2.8vw;
		user-select: none;
		cursor: pointer;
	}

	/***************business****************/
	.wrapper .business {
		width: 100%;
		margin-top: 12vw;
		padding-bottom: 14vw;
	}

	.wrapper .business li {
		width: 100%;
		box-sizing: border-box;
		padding: 2.5vw;
		border-bottom: solid 1px #DDDDDD;
		user-select: none;
		cursor: pointer;

		display: flex;
		align-items: center;
	}

	.wrapper .business li .bsns-img {
		position: relative;
	}

	.wrapper .business li .bsns-img img {
		width: 20vw;
		height: 20vw;
	}

	.wrapper .business li .bsns-img .bsns-img-quantify {
		width: 5vw;
		height: 5vw;
		background-color: red;
		color: #FFFFFF;
		font-size: 3.6vw;
		border-radius: 2.5vw;
		
		display: flex;
		justify-content: center;
		align-items: center;

		position: absolute;
		right: -1.5vw;
		top: -1.5vw;
	}

	.wrapper .business li .bsns-info {
		margin-left: 3vw;
	}

	.wrapper .business li .bsns-info h3 {
		font-size: 3.8vw;
		color: #555555;
		margin-bottom: 3.4vw;
	}

	.wrapper .business li .bsns-info p {
		font-size: 3vw;
		color: #888888;
		margin-top: 2vw;
	}
</style>
